"""
Case Type   : 兼容日期函数
Case Name   : adddate(date, interval exper unit)函数第一参数输入time类型参数测试边界值，第二参数输入正确
Create At   : 2023.03.30
Owner       :@zou_jialiang0501129488
Description :
    1、创建兼容b库并查询插件是否加载成功
    2、adddate(date, interval exper unit)函数第一参数输入time类型参数测试边界值，第二参数输入正确
    3、清理环境
Expect      :
    1、成功，插件查询成功
    2、成功
    3、清理成功
History     :modified by zhanghuan96，2024.8.26，IAHP3M问题修复，代码变更
"""

import os
import unittest

from yat.test import Node
from yat.test import macro

from testcase.utils.Common import Common
from testcase.utils.CommonSH import CommonSH
from testcase.utils.Constant import Constant
from testcase.utils.Logger import Logger


class CompatibilityTest01(unittest.TestCase):

    def setUp(self):
        self.log = Logger()
        self.log.info(f'-----{os.path.basename(__file__)} start-----')
        self.common = Common()
        self.sh_primary = CommonSH('PrimaryDbUser')
        self.constant = Constant()
        self.db_name = 'db_adddate_0024'

    def test_create_function(self):
        text = '-----step1:创建兼容b库库并查询插件;expect:成功-----'
        self.log.info(text)
        sql_cmd = f"drop database if exists {self.db_name};" \
                  f"create database {self.db_name} dbcompatibility ='B';"
        self.log.info(sql_cmd)
        sql_res = self.sh_primary.execut_db_sql(sql_cmd)
        self.log.info(sql_res)
        self.assertIn(self.constant.CREATE_DATABASE_SUCCESS,
                      sql_res, '兼容B库创建失败')

        sql_cmd1 = "select extname from pg_extension where extname ='dolphin';"
        self.log.info(sql_cmd1)
        sql_res1 = self.sh_primary.execut_db_sql(sql_cmd1, dbname=self.db_name)
        self.log.info(sql_res1)
        self.assertIn('dolphin', sql_res1, '查询失败')

        text = '-----step2:adddate(date, interval exper unit)函数第一参数输入time类型' \
               '参数测试边界值，第二参数输入正确:truncate后计算返回-----'
        self.log.info(text)
        sql_cmd2 = f"set dolphin.support_interval_to =on;" \
                   f"select adddate(time'838:59:59', interval -1 day);" \
                   f"select adddate(time'838:59:59', interval -2 hour);" \
                   f"select adddate(time'838:59:59', interval -3 minute);" \
                   f"select adddate(time'838:59:59', interval -4 second);" \
                   f"select adddate(time'-838:59:59', interval 5 day);" \
                   f"select adddate(time'-838:59:59', interval 6 hour);" \
                   f"select adddate(time'-838:59:59', interval 7 minute);" \
                   f"select adddate(time'-838:59:59', interval 8 second);" \
                   f"select adddate(" \
                   f"time'838:59:59', interval -9 day to hour);" \
                   f"select adddate(" \
                   f"time'-838:59:59', interval 10 day to minute);" \
                   f"select adddate(" \
                   f"time'838:59:59', interval -11 day to second);" \
                   f"select adddate(" \
                   f"time'-838:59:59', interval 12 hour to minute);" \
                   f"select adddate(" \
                   f"time'838:59:59', interval -13 hour to second);" \
                   f"select adddate(" \
                   f"time'-838:59:59', interval 14 minute to second);"
        self.log.info(sql_cmd2)
        res = self.sh_primary.execut_db_sql(sql_cmd2, dbname=self.db_name)
        self.log.info(res)
        msg_lis = ['814:59:59', '836:59:59', '838:56:59', '838:59:55',
                   '-718:59:59', '-832:59:59', '-838:52:59', '-838:59:51',
                   '829:59:59', '-838:49:59', '838:59:48', '-838:47:59',
                   '838:59:46', '-838:59:45']
        for msg in msg_lis:
            self.assertIn(msg, res, '日期函数查询失败')

    def tearDown(self):
        text = '-----step3:清理环境;expect:成功-----'
        self.log.info(text)
        sql_cmd4 = f"drop database if exists {self.db_name};"
        self.log.info(sql_cmd4)
        sql_res4 = self.sh_primary.execut_db_sql(sql_cmd4)
        self.log.info(sql_res4)
        self.assertIn(self.constant.DROP_DATABASE_SUCCESS,
                      sql_res4, '清理环境失败')
        self.log.info(f'-----{os.path.basename(__file__)} end-----')
